package com.bing.habit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bing.habit.entity.Habit;
import com.bing.habit.mapper.ClockinRecordDAO;
import com.bing.habit.entity.ClockinRecord;
import com.bing.habit.service.ClockInRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
public class ClockInRecordServiceImpl implements ClockInRecordService {

    @Autowired
    ClockinRecordDAO clockinRecordDAO;

    @Override
    public void addOrUpdate(ClockinRecord clockinRecord) {
        //修改
        if (clockinRecord.getId() != 0) {
            clockinRecordDAO.updateById(clockinRecord);
        } else {
            clockinRecordDAO.insert(clockinRecord);
        }
    }

    @Override
    public void delete(int id) {
        clockinRecordDAO.deleteById(id);
    }

    /**
     * 根据打卡记录的存在判断今天是否打卡
     *
     * @param hid
     * @return
     */
    @Override
    public boolean listByToday(int hid) {
        if (clockinRecordDAO.listToday(hid).size() > 0) {
            return true;
        }
        return false;
    }

    /**
     * 根据昨天打卡记录是否存在判断 连续打卡
     *
     * @param hid
     * @return
     */
    @Override
    public boolean listByYesterday(int hid) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, -24);
        String yesterdayDate = dateFormat.format(calendar.getTime());
        if (clockinRecordDAO.listDay(hid, yesterdayDate).size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public ClockinRecord listTodayIn(int hid) {
        return clockinRecordDAO.listTodayIn(hid);
    }

    @Override
    public List<Integer> listWeeks(int hid) {
        return clockinRecordDAO.listWeeks(hid);
    }

    @Override
    public List<Integer> listMonths(int hid, String date) {
        List arr = new ArrayList();
        List<ClockinRecord> crs = clockinRecordDAO.listMonths(hid, date);
        for (ClockinRecord cr : crs) {
            arr.add(String.format("%td", cr.getClockinTime()));
        }
        return arr;
    }

    /**
     * @description: 根据习惯id查询打卡记录
     * @author: xujiabing
     * @date: 2024-09-28 22:04
     * @param: habitId
     * @return: java.util.List<com.bing.habit.entity.ClockinRecord>
     */
    @Override
    public List<ClockinRecord> queryByHabitId(int habitId) {
        LambdaQueryWrapper<ClockinRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ClockinRecord::getHid,habitId);
        return clockinRecordDAO.selectList(wrapper);
    }

    /**
     * @description: 批量删除操作
     * @author: xujiabing
     * @date: 2024-09-29 8:58
     * @param: ids
     * @return: void
     */
    @Override
    public void deleteBatchIds(List<Integer> ids) {
        clockinRecordDAO.deleteBatchIds(ids);
    }
}
